package com.hysteria.game;
import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;

import com.hyteria.render.Renderable;
import com.hyteria.render.Renderer;



public class Cell extends Renderable
{
	public final static int cellSize=32;
	public enum CellType{
		GROUND,WALL;
	}
	
	private CellType type;
	private ArrayList<Entity> entityList; //This array contains all of the entities that are contained within this cell.
	
	public Cell(CellType c, int x, int y)
	{
		entityList = new ArrayList<Entity>();			
		setX(x*cellSize);
		setY(y*cellSize);
		setCellType(c);
		Renderer.registerRenderable(this);
	}

	public int getGridX(){return Math.round(getX()/cellSize);}
	public int getGridY(){return Math.round(getY()/cellSize);}
	public boolean isCollidable(){return (type == CellType.WALL);}
	public CellType getCellType(){return this.type;}
	public void setCellType(CellType c){ 
		this.type=c;
		
		if(type == CellType.WALL){
			try{
				//this.setImage(new Image("res/ground1.png"));
				setShape(new Rectangle(getX(), getY(), cellSize, cellSize));
				setColor(Color.gray);
			}catch(Exception e){}
		}else if(type ==CellType.GROUND){
			try{
				//this.setImage(new Image("res/wall.png"));
				setShape(new Rectangle(getX(), getY(), cellSize, cellSize));
				setColor(Color.blue);
			}catch(Exception e){}			
		}
	}
	public void putEntityInCell(Entity e){
		entityList.add(e);
		e.notifyCellPlacement(this);
	}
	public void removeEntityFromCell(Entity e){
		entityList.remove(e);
		e.removeFromCell();
	}
	public boolean contains(Entity e){
		return entityList.contains(e);
	}
	public ArrayList<Entity> getEntityList(){
		return entityList;
	}
	
	
	public String toString(){
		String s = new String();
		s += "Cell type:\t" + ((type == CellType.WALL) ? "Wall" : "Ground") + "\n";
		s += "isCollidable():\t" + isCollidable() + "\n";
		s += "Grid pos:\t" + getGridX() + ", " + getGridY() + "\n";
		s += "World pos:\t" + getX() +", " + getY() + "\n";
		return s;
	}
}
